import win32print
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.QtCore import QObject, pyqtSlot, QUrl, QSizeF, QMarginsF, QTimer
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtPrintSupport import QPrinter
import sys

################################################
the_html='''
<!-- webprint.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>使用PyQt5打印热敏小票</title>
</head>
<style type="text/css">
    * {padding:0;margin: 0;font-weight:bold;face="KaiTi"}
    h1 {font-size: 1em}
    h3 {text-align: center;font-size: 2em}
    .left {float: left;}
    .right {float:right;}
    .clearfix {clear: both;}
    .print_container {width: 180px;}
    .section2 label {font-size: 1em;display: block}
    .section3 label {font-size: 1em;display: block}
    .section4 .total label {font-size: 1em;display: block}
    .section4 {text-align: center;font-size: 1em;border-bottom: 1em solid #DADADA}
    .section5 label {text-align: center;font-size: 1em;display: block}
</style>
<body>
<div id="capture">
    <div class="print_container">
        <h3>一心狮便利店</h3>
        <span>***************************************</span>
        <div class="section3">
            <label>订单号：700001001201811161631123558</label>
            <label>下单时间：2018-10-16 16:31:14</label>
            <label>收银员：王小明</label>
        </div>
        <span>***************************************</span>
        <div class="section4">
            <div style="border-bottom: 3px solid #DADADA;">
                <table style="width: 180;">
                    <thead>
                    <tr>
                        <td width="110">商品名</td>
                        <td width="40">单价</td>
                        <td width="30">数量</td>

                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <td>今麦郎1</td>
                        <td>100</td>
                        <td>10000.00</td>
                    </tr>
                    <tr>
                        <td>今麦郎2</td>
                        <td>1</td>
                        <td>100.00</td>
                    </tr>
                    <tr>
                        <td>今麦郎3</td>
                        <td>1</td>
                        <td>100.00</td>
                    </tr>
                    </tbody>
                </table>
            </div>
            <div class="total">
                <label class="left">合 计</label>
                <label class="right">100.00</label>
                <div class="clearfix"></div>
                <label class="left">收款金额</label>
                <label class="right">100</label>
                <div class="clearfix"></div>
                <label class="left">找零金额</label>
                <label class="right">0.00</label>
                <div class="clearfix"></div>
            </div>
            <div style="text-align: centre;">
                <label>顾客已付款</label>
            </div>
            <span>***************************************</span>
        </div>
        <div class="section5">
            <label>电话：</label>
        </div>
        <span>***************************************</span>
        <div class="section5">
            <label>欢迎光临，谢谢惠顾！</label>
            <label>一心狮便利店</label>            
        </div>
        <div align="center" class="section5">
              <br />
              <img src='https://ncstatic.clewm.net/feres/a44fa42f_4b16ec36_1666157560.png' align="middle" width="130" height="130"/>
              <div class="clearfix"></div>
              <br />
              <label>微信扫码 购好物 享服务</label>
        </div> 
        
    </div>
    
</div>

</body>
</html>
'''

################################################
# 打印回调函数
def print_completed(result):
    pass

# 开始打印小票
def goto_print_litbill():
    webview.page().print(printer, print_completed)


################################################
#######程序入口
################################################
if  __name__ == '__main__':
    app = QApplication(sys.argv)
    webview = QWebEngineView()
    webview.setHtml(the_html)
    #webview.show()
    win32print.SetDefaultPrinter("pos58")  #设置为默认打印机
    printer = QPrinter()
    printer.setPageSizeMM(QSizeF(80, 300))
    ##########必须延迟打印，否则网页还没加载，打印空白#########
    timer = QTimer()
    timer.timeout.connect(goto_print_litbill)
    timer.setSingleShot(True)
    timer.start(500)
    ###################################################
    sys.exit(app.exec_())